package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.constant.RedisConstant;
import com.itheima.domain.Address;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.mapper.AddressMapper;
import com.itheima.mapper.Order_Address_Mapper;
import com.itheima.service.AddressService;
import com.itheima.util.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.List;

@Service(interfaceClass = AddressService.class)
@Transactional
public class AddressServiceImpl implements AddressService {


    @Autowired
    private AddressMapper addressMapper;

    @Autowired
    private Order_Address_Mapper order_address_mapper;

    @Autowired
    private JedisPool jedisPool;

    @Override
    public PageResult findWithPage(QueryPageBean bean) {

        String queryString = bean.getQueryString();
        Integer pageNum = bean.getCurrentPage();
        Integer pageSize = bean.getPageSize();

        PageHelper.startPage(pageNum, pageSize);

        List<Address> addresses = addressMapper.findWithPage(queryString);

        PageInfo<Address> info = new PageInfo<>(addresses);

        return new PageResult(info.getTotal() , info.getList());
    }


    @Override
    public void addAddress(Address address) {


        addressMapper.insertAddress(address);

        //添加成功之后就需要进行 redis 操作

        if (address.getImage() != null) {

            Jedis jedis = jedisPool.getResource();

            jedis.sadd(RedisConstant.IMAGE_URL_ADDRESS_DB , address.getImage());

            jedis.close();

        }




    }

    @Override
    public void deleteAddress(int addressId) {

            Address address = addressMapper.selectById(addressId);

            Jedis jedis = jedisPool.getResource();

            if(address.getImage() != null && address.getImage().length() > 0){

                //对图片进行清理

                jedis.srem(RedisConstant.IMAGE_URL_ADDRESS_DB , address.getImage());
                jedis.srem(RedisConstant.IMAGE_URL_ADDRESS_QINIU , address.getImage());

                QiniuUtils.deleteFileFromQiniu(address.getName());
            }

            //先删除中间表
            order_address_mapper.deleteByAddressId(addressId);

            addressMapper.deleteById(addressId);

            jedis.close();


        }

    @Override
    public Address findById(int addressId) {


        return addressMapper.selectById(addressId);


    }

    @Override
    public void updateAddress(String hasOldImg, Address address) {
        Jedis jedis = jedisPool.getResource();

        if(address.getImage() != null && address.getImage().length() > 0){
            //本次上传了图片

            if (hasOldImg != null && hasOldImg.length() > 0){
                //原来有图片

                //那么就需要将原来的图片删除
                jedis.srem(RedisConstant.IMAGE_URL_ADDRESS_QINIU , hasOldImg);
                jedis.srem(RedisConstant.IMAGE_URL_ADDRESS_DB , hasOldImg);
                QiniuUtils.deleteFileFromQiniu(hasOldImg);

            }

            //即是这次没有上传图片也快可以存到 redis 中 因为 set集合
            jedis.sadd(RedisConstant.IMAGE_URL_ADDRESS_DB , address.getImage());

        }

        //进行数据的更新
        addressMapper.updateAddress(address);

        //资源关闭
        jedis.close();

    }

    @Override
    public List<Address> findAll() {

        return addressMapper.findAll();


    }


}
